package au.com.bus4u.controller;

import au.com.bus4u.service.OrderService;
import au.com.bus4u.service.ShuttleBusOrderService;
import au.com.bus4u.service.UserService;
import au.com.bus4u.service.UserShuttleBusOrderService;
import au.com.bus4u.utils.PageData;
import lombok.extern.java.Log;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Log
public class HtmlController extends BaseController  {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShuttleBusOrderService shuttleBusOrderService;
    @Autowired
    private UserShuttleBusOrderService userShuttleBusOrderService;

    @Value("${min-auction-amount}")
    private Double minAuctionAmount;

    //跳转登录页面
    @RequestMapping({"/login.html","/toLogin"})
    public ModelAndView toLogin()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Login");//页面title
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/login.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转注册页面
    @RequestMapping({"/regist.html","/toRegist"})
    public ModelAndView toRegist()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Regist");//页面title
        ModelAndView mv = new ModelAndView();
        mv.addObject("save_url","/pc/user/regist");
        mv.setViewName("/pc/regist.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转服务条款页面
    @RequestMapping({"/theTermsOfService.html","/toTermsOfService"})
    public ModelAndView totheTermsOfService()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","The Terms of Service");//页面title
        ModelAndView mv = new ModelAndView();
//        mv.addObject("save_url","/pc/user/regist");
        mv.setViewName("/pc/theTermsOfService.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转旧用户数据补全页面
    @RequestMapping({"/oldUserInfoSupplement.html","/oldUserInfoSupplement"})
    public ModelAndView toOldUserInfoSupplement()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","OldUserInfoSupplement");//页面title
        ModelAndView mv = new ModelAndView();
        mv.addObject("save_url","/pc/user/oldUserInfoSupplement");
        mv.setViewName("/pc/oldUserInfoSupplement.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转首页
    @RequestMapping({"/","/index","/index.html"})
    public ModelAndView toIndex()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Home");//页面title
        map.put("active",0);//选中页面菜单标识
        map.put("currentUser",getCurrentUser());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/index.html");
        mv.addObject("data",map);
//        System.out.println("role_id = " + getCurrentUser().get("role_id"));
        mv.addObject("role_id", getCurrentUser().get("role_id"));
        return mv;
    }
    //跳转home页面
//    @RequestMapping({"home/home.html","/toHome"})
//    public ModelAndView toHome()throws Exception{
//        ModelAndView mv = new ModelAndView();
//        mv.setViewName("/pc/home/home.html");
//        return mv;
//    }
    //跳转订单页面
    @RequestMapping({"home/order.html","/toOder"})
    public ModelAndView toOder(HttpServletRequest request)throws Exception{
        String order_type = "0";
        if(!(request.getQueryString()==null)){
            order_type = request.getParameter("order_type");
        }else{
             order_type = "0";
        }
        ModelAndView mv = new ModelAndView();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Order");//页面title
        map.put("active",1);
        //根据 order_type  查询 订单
        List<PageData> orderList = null;
        if(order_type==null || order_type.equals("0")){
            orderList = orderService.findByUserId(getCurrentUserID(), null, null,null);
        }else if(order_type.equals("1")){
            orderList = userShuttleBusOrderService.list(null);
        }else if(order_type.equals("2")){

        }

        if(orderList.size()>0){
            map.put("orderList",orderList);
        }else {
            map.put("orderList",null);
        }
        map.put("orderListLength",orderList.size());

        mv.setViewName("/pc/home/order.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转 新增订单页面
    @RequestMapping({"home/addOrder.html","/addOrder"})
    public ModelAndView addOrder()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","addOrder");//页面title
        map.put("active",-1);
        map.put("currentUser",getCurrentUser());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/addOrder.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转 修改订单页面
    @RequestMapping({"home/editOrder.html","/editOrder"})
    public ModelAndView editOrder(HttpServletRequest request)throws Exception{
        log.info("=========================================");
        Integer order_id = Integer.parseInt( request.getParameter("order_id"));
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","editOrder");//页面title
        map.put("active",-1);
        map.put("currentUser",getCurrentUser());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/editOrder.html");
        mv.addObject("data",map);
        mv.addObject("order",orderService.findById(order_id));
        return mv;
    }
    //跳转shuttle页面
    @RequestMapping({"home/shuttle.html","/toShuttle"})
    public ModelAndView toShuttle()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Shuttle");//页面title
        map.put("active",2);
        //从数据库中获取所有竞拍订单数据
        PageData pd = new PageData();
        pd.put("order_status",0);//订单状态

        map.put("shuttleList",shuttleBusOrderService.list(pd));
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/shuttle.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转charter页面
    @RequestMapping({"home/charter.html","/toCharter"})
    public ModelAndView toCharter()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Charter");//页面title
        map.put("active",3);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/charter.html");
        mv.addObject("data",map);
        return mv;
    }

    //跳转auctionOrder页面
    @RequiresRoles(value={"SuperAdmin","Admin","Carrier","Driver"},logical = Logical.OR)
    @RequestMapping({"home/auctionOrder.html","/toAuctionOrder"})
    public ModelAndView toAuctionOrder()throws Exception{
        log.info("=========================================");
//        System.err.println("HtmlController.toAuctionOrder");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Auction");//页面title
        map.put("active",4);
        ModelAndView mv = new ModelAndView();
        mv.addObject("data",map);

        //从数据库中获取所有竞拍订单数据
        PageData pd = new PageData();
        pd.put("order_status",4);//订单状态
        pd.put("order_type",0);//订单类型 正常0-Normal 和 班车1-Shuttle

        List<PageData> orderList = orderService.list(pd);
//        System.err.println("orderList = " + orderList);
        map.put("orderList",orderList);
        map.put("orderListLength",orderList.size());

        mv.setViewName("/pc/home/auctionOrder.html");
        mv.addObject("data",map);
        mv.addObject("auctionCount",0);
        mv.addObject("minAuctionAmount",minAuctionAmount);
        return mv;
    }
    //跳转cashier页面
    @RequestMapping({"home/cashier.html","/toCashier"})
    public ModelAndView toCashier()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Cashier");//页面title
        map.put("active",5);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/cashier.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转contactUs页面
    @RequestMapping({"home/contactUs.html","/toContactUs"})
    public ModelAndView toContactUs()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","ContactUs");//页面title
        map.put("active",-1);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/contactUs.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转aboutUs页面
    @RequestMapping({"home/aboutUs.html","/toAboutUs"})
    public ModelAndView toAboutUs()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","AboutUs");//页面title
        map.put("active",-1);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/aboutUs.html");
        mv.addObject("data",map);
        return mv;
    }
    //跳转readmeBeforeUse页面
    @RequestMapping({"home/readmeBeforeUse.html","/toReadmeBeforeUse"})
    public ModelAndView toReadmeBeforeUse()throws Exception{
        log.info("=========================================");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","ReadmeBeforeUse");//页面title
        map.put("active",-1);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/home/readmeBeforeUse.html");
        mv.addObject("data",map);
        return mv;
    }

    //跳转未授权页面页面 无权限跳转
    @RequestMapping({"/unAuth.html","/unAuth"})
    public ModelAndView toUnAuth()throws Exception{
        log.info("=========================================");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/pc/unAuth.html");
        return mv;
    }

    //错误页面跳转
//    @RequestMapping({"/error.html","/error"})
//    public ModelAndView toError()throws Exception{
//        ModelAndView mv = new ModelAndView();
//        mv.setViewName("/error.html");
//        return mv;
//    }


    //post 登录重定向 首页
//    @PostMapping(value = "/index.do")
//    public RedirectView submit(RedirectAttributes redirectAttributes,@RequestParam("user_email") String user_email,@RequestParam("password") String password,@RequestParam("code") String code) {
//        HttpSession session = request.getSession();
//        if(code.equals("1111")){
//            if(user_email.equals("root") && password.equals("123456")){
//                RedirectView redirectTarget = new RedirectView();
//                redirectTarget.setContextRelative(true);
//                redirectTarget.setUrl("/index.html");
//                return redirectTarget;
//            }else{
//                RedirectView redirectTarget = new RedirectView();
//                redirectTarget.setContextRelative(true);
//                redirectTarget.setUrl("/login.html");
//                redirectAttributes.addFlashAttribute("errorMessage", "Email or Password incorrect");
//                return redirectTarget;
//            }
//        }else {
//            RedirectView redirectTarget = new RedirectView();
//            redirectTarget.setContextRelative(true);
//            redirectTarget.setUrl("/login.html");
//            redirectAttributes.addFlashAttribute("errorMessage", "Code incorrect");
//            return redirectTarget;
//        }
//    }

}
